import 'package:flutter_test/flutter_test.dart';
import 'package:util/result/catching.dart';
import 'package:util/result/result_extension.dart';

void _newTestResult() {
  group('new test result', () {
    test('ss', () async {
      final result = runCatching(() async {
        await Future.delayed(Duration(microseconds: 50));
        return 1;
      });
      expect(result.isOk, true);
      expect(result.getOrNull(), 1);
    });
  });
}

void main() {
  _newTestResult();
  group('test result', () {
    test('should be ok', () {
      final testValue = 's';
      final result = runCatching(() {
        return testValue;
      });

      expect(result.isOk, true);
      expect(result.getOrNull(), testValue);
    });

    test('should be ok in async', () async {
      final result = await runCatchingAsync(() async {
        await Future.delayed(Duration(milliseconds: 50));
        return "1";
      });

      expect(result.isOk, true);
      expect(result.getOrNull(), '1');
    });

    test('should be error', () {
      final result = runCatching(() {
        throw Exception('s');
      });

      expect(result.isError, true);
      expect(result.exceptionOrNull(), isNotNull);
      expect(result.exceptionOrNull(), isA<Exception>());
    });

    test('should be error in async', () async {
      final result = await runCatchingAsync(() async {
        await Future.delayed(Duration(milliseconds: 50));
        throw Exception('s');
      });

      expect(result.isError, true);
      expect(result.exceptionOrNull(), isNotNull);
      expect(result.exceptionOrNull(), isA<Exception>());
    });
  });
}
